﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Web.myadmin
{
    public class SessionHelp
    {

        /// <summary>
        /// 获取登录用户
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static BZZ.Model.adminUsers GetLoginUser(System.Web.UI.Page page)
        {
            //BZZ.Model.adminUsers mod = new BZZ.Model.adminUsers();
            //mod.id = 1;
            //mod.username = "admin";
            //page.Session["LoginUser"] = mod;

            if (page.Session["LoginUser"] != null)
            {
                return (BZZ.Model.adminUsers)page.Session["LoginUser"];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 保存登录用户
        /// </summary>
        /// <param name="page"></param>
        /// <param name="loginUser"></param>
        public static void SetLoginUser(System.Web.UI.Page page, BZZ.Model.adminUsers loginUser)
        {
            page.Session["LoginUser"] = loginUser;
        }
        /// <summary>
        /// 保证已登录，否则重定向到登录页面
        /// </summary>
        /// <param name="page"></param>
        public static void AssertLogin(System.Web.UI.Page page)
        {
            if (GetLoginUser(page) == null)
            {
                page.Response.Write("<script language='javascript'>top.location.href='/bzzAdmin/login.aspx';</script>");
                page.Response.End();
            }
        }
        public static bool isLogin(System.Web.UI.Page page)
        {
            if (GetLoginUser(page) == null) return false;
            else return true;
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="page"></param>
        public static void LogOut(System.Web.UI.Page page)
        {
            page.Session.Clear();
            page.Response.Cookies["LoginUser"].Expires = DateTime.Now.AddDays(-1);
            page.Response.Write("<script language='javascript'>top.location.href='/Default.aspx';</script>");
            page.Response.End();
        }
    }

}
